﻿using ImoviesBlazor.Server.Data;
using ImoviesBlazor.Server.Helpers;
using ImoviesBlazor.Server.Models;
using ImoviesBlazor.Server.Repository;
using ImoviesBlazor.Server.Repository.PlayLink;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

ConfigurationManager configuration = builder.Configuration;

// 添加dbContex
builder.Services.AddDbContext<ApplicationDbContext>(options =>
             options.UseSqlite(configuration.GetConnectionString("SqliteConnection")));

builder.Services.AddIdentity<ApplicationUser, IdentityRole<Guid>>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

// 电视剧仓储服务
builder.Services.AddScoped<ITeleplayRepository, TeleplayRepository>();
// 电影仓储服务
builder.Services.AddScoped<IMovieRepository, MovieRepository>();
// 动漫仓储服务
builder.Services.AddScoped<IAnimationRepository, AnimationRepository>();
// 年度评论仓储服务
builder.Services.AddScoped<IAnnualReviewRepository, AnnualReviewRepository>();

// 电视剧链接仓储服务
builder.Services.AddScoped<ITeleplayVlinkRepository, TeleplayVlinkRepository>();
// 电影链接仓储服务
builder.Services.AddScoped<IMovieVlinkRepository, MovieVlinkRepository>();
// 动漫链接仓储服务
builder.Services.AddScoped<IAnimationVlinkRepository, AnimationVlinkRepository>();

// 播放链接仓储服务
builder.Services.AddScoped<IPlaylinkRepository, PlaylinkRepository>();

// automapper配置
builder.Services.AddAutoMapper(typeof(MyMappingProfile));


// Identity配置
builder.Services.Configure<IdentityOptions>(options =>
{
    // Password settings
    options.Password.RequireDigit = true;
    options.Password.RequiredLength = 6;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;

    // Lockout settings
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
    options.Lockout.MaxFailedAccessAttempts = 5;
    options.Lockout.AllowedForNewUsers = true;

    // User settings
    options.SignIn.RequireConfirmedAccount = false;
    options.User.RequireUniqueEmail = false;
});

builder.Services.ConfigureApplicationCookie(options =>
{
    options.Cookie.HttpOnly = true;
    options.Events.OnRedirectToLogin = context =>
    {
        context.Response.StatusCode = 401;
        return Task.CompletedTask;
    };
});


// NewtonsoftJson
//builder.Services.AddControllersWithViews().AddNewtonsoftJson(option =>
//{
//    // 忽略循环引用
//    option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//});


// System.Text.Json配置
builder.Services.AddControllersWithViews().AddJsonOptions(o =>
{
    // 包含字段
    o.JsonSerializerOptions.IncludeFields = true;
    // 属性名称不区分大小写
    o.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});


builder.Services.AddRazorPages();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    //using (var serviceScope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
    //{
    //    //Note: Microsoft recommends to NOT migrate your database at Startup. 
    //    //You should consider your migration strategy according to the guidelines.
    //    serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
    //}

    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();
